//
//  NSObject+mutable.m
//  ebook
//
//  Created by lj on 14-9-3.
//  Copyright (c) 2014年 cczone. All rights reserved.
//

#import "NSObject+mutable.h"

@implementation NSObject (mutableobj)

-(NSObject *)allMutableCopy
{
    if([self isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)self;
        NSMutableArray *result = [NSMutableArray arrayWithCapacity:arr.count];
        for(NSObject *obj in arr)
        {
            [result addObject:[obj allMutableCopy]];
        }
        return result;
    }
    else if ([self isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dic = (NSDictionary *)self;
        NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:dic.count];
        for(NSObject<NSCopying> *key in dic)
        {
            result[key] = [dic[key] allMutableCopy];
        }
        return result;
    }
    return self;
}

-(void)removeNullData
{
    if([self isKindOfClass:[NSMutableDictionary class]])
    {
        NSMutableDictionary *dic = (NSMutableDictionary *)self;
        NSArray *keys = [dic allKeys];
        for(id key in keys)
        {
            if([dic[key] isKindOfClass:[NSNull class]])
            {
                [dic removeObjectForKey:key];
            }
            else
            {
                [dic[key] removeNullData];
            }
        }
    }
    else if ([self isKindOfClass:[NSMutableArray class]])
    {
        NSMutableArray *arr = (NSMutableArray *)self;
        for(id object in arr)
        {
            [object removeNullData];
        }
    }
}

@end
